/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package modele;

import exception.InsuranceNotExists;
import exception.InsuranceExists;
import exception.NoInsuranceForThisCar;
import exception.NoDamageHistory;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.Locale;

/**
 *
 * @author kluck
 */
public class Customer {
    protected Integer idCustomer;
    protected String firstName;
    protected String lastName;
    protected Collection<Insurance> insurances;
    protected Collection<Claim> damageHistory;
    
    public Customer(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.insurances = new ArrayList<>();
        /* If we want to use an ArrayList, we have to modify add and remove
         because we don't want 2 identical claims in damageHistory */
        this.damageHistory = new HashSet<>();
    }
    
    public String getFirstName() {
        return this.firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return this.lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Collection<Insurance> getInsurances() {
        return insurances;
    }

    public void setInsurances(Collection<Insurance> insurances) {
        this.insurances = insurances;
    }
    
    public void addInsurance( Insurance insurance ) throws InsuranceExists {
        if ( insurances.contains(insurance) )
            throw new InsuranceExists( insurance );
        else
            insurances.add(insurance);
    }
    
    public void delInsurance( Insurance insurance ) throws InsuranceNotExists {
        if ( !insurances.contains(insurance) )
            throw new InsuranceNotExists( insurance );
        else
            insurances.remove(insurance);
    }

    public Integer getIdCustomer() {
        return this.idCustomer;
    }
    
    public void setIdCustomer(Integer id) {
        this.idCustomer = id;
    }

    public Collection<Claim> getDamageHistory() {
        return this.damageHistory;
    }

    public void addClaim(Claim claim) {
        this.damageHistory.add(claim);
    }
    
    public void delClaim(Claim claim) {
        this.damageHistory.remove(claim);
    }
    
    public void addDamageHistory(Collection<Claim> damageHistory) {
        this.damageHistory = damageHistory;
    }
    
    public Claim getLastClaim() throws NoDamageHistory {
        GregorianCalendar gc = new GregorianCalendar(2000, 0, 1);
        Claim resultClaim = new Claim();
        if (this.getDamageHistory().isEmpty()) {
            throw new NoDamageHistory();
        }
        for (Claim claim : damageHistory) {
            if (claim.getAccidentDate().compareTo(gc)>=0) {
                gc=claim.getAccidentDate();
                resultClaim = claim;
            }
        }
        return resultClaim;
    }
    
    public Insurance checkInsurance(String carRegistration) throws
            NoInsuranceForThisCar {
        for ( Insurance ins : insurances ) {
            if ( ins.getCarRegistration().equalsIgnoreCase(carRegistration) ) {
                return ins;
            }
        }
        throw new NoInsuranceForThisCar( carRegistration );
    }
    
    public String checkDamageHistoryText(Claim claim) throws NoDamageHistory {
        Float totalDamage = new Float(0.0f);
        GregorianCalendar previousYear = new GregorianCalendar(Locale.FRANCE);
        previousYear = (GregorianCalendar) claim.getAccidentDate().clone();
        previousYear.add(Calendar.YEAR, -1);
        
        StringBuilder str = new StringBuilder("");
        
        for (Claim c : this.getDamageHistory()) {
            /* If the accident date is less than a year before the examinated claim */
            if (c.getAccidentDate().after(previousYear) &&
                    c.getAccidentDate().before(claim.getAccidentDate())) {
                totalDamage+=c.getExpensesTotalPaidByCustomer();
                String responsability = c.getResponsibility() ==
                        Claim.Responsibility.RESPONSIBLE ? "responsible" : 
                        "not responsible";
                str.append("\nClaim ID : ").append(c.getIdClaim()).
                        append("\n\tResponsability : ").append(responsability).
                        append("\n\tExpenses : ").
                        append(c.getExpensesTotalPaidByCustomer()).append("\n");
                }
        }
        if (str.toString().equals("")) {
            throw new NoDamageHistory();
        } else {
            str.append("\n################\nTotal expenses : ").append(totalDamage);
        }
        return str.toString();
    }
    
    public String toString() {
        return firstName + " " + lastName;
    }
    
}
